<!--
 * @Descripttion: 交易设置
 * @Author: xiao li
 * @Date: 2020-09-28 15:24:24
 * @LastEditors: xiao li
 * @LastEditTime: 2022-09-15 23:46:51
-->
<template>
  <div class="lb-system-transaction">
    <top-nav />
    <div class="page-main">
      <el-row class="page-top-operate">
        <lb-button
          size="medium"
          type="primary"
          icon="el-icon-plus"
          style="margin-right: 5px"
          @click="addCityList"
          >{{ $t('action.addCity') }}</lb-button
        >
      </el-row>
      <el-table
        v-loading="loading"
        :data="cityList"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="title" label="城市"></el-table-column>
        <el-table-column prop="lng" label="经度"></el-table-column>
        <el-table-column prop="lat" label="纬度"></el-table-column>
        <!-- <el-table-column prop="status" label="状态"></el-table-column> -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div>
              <lb-button
                size="mini"
                plain
                type="primary"
                @click="editList(scope.row)"
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="confirmDel(scope.row)"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
    <el-dialog
      :title="subForm.id ? '编辑城市' : '新增城市'"
      :visible.sync="dialogVisible"
      width="500px"
      center
    >
      <el-form
        @submit.native.prevent
        :model="subForm"
        ref="subForm"
        :rules="subFormRules"
        label-width="60px"
      >
        <el-form-item label="城市" prop="title">
          <el-input
            v-model="subForm.title"
            placeholder="请输入城市名称"
            maxlength="10"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="经度" prop="lng">
          <el-input v-model="subForm.lng" placeholder="请输入经度"></el-input>
        </el-form-item>
        <el-form-item label="纬度" prop="lat">
          <el-input v-model="subForm.lat" placeholder="请输入纬度"></el-input>
        </el-form-item>
        <el-form-item label="" prop="">
          <lb-button
            class="mr-md"
            @click="showMap = true"
            plain
            type="primary"
            >{{ $t('action.getLatLng') }}</lb-button
          >
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">{{
          $t('action.cancel')
        }}</el-button>
        <el-button type="primary" @click="submitForm">{{
          $t('action.comfirm')
        }}</el-button>
      </span>
    </el-dialog>
    <lb-map
      :address="subForm.title"
      :dialogVisible.sync="showMap"
      @selectedLatLng="getLatLng"
    ></lb-map>
  </div>
</template>

<script>
export default {
  data () {
    return {
      total: 0,
      loading: false,
      dialogVisible: false,
      showMap: false,
      searchForm: {
        page: 1,
        limit: 10
      },
      subForm: {
        title: '',
        lng: '',
        lat: ''
      },
      cityList: [],
      subFormRules: {
        title: {
          required: true,
          message: '请输入城市名称',
          trigger: 'blur'
        },
        lng: {
          required: true,
          message: '请输入经度',
          validator: this.$reg.isLng
        },
        lat: {
          required: true,
          message: '请输入纬度',
          validator: this.$reg.isLat
        }
      }
    }
  },
  watch: {

  },
  created () {
    this.getList()
  },
  methods: {
    editList (row) {
      this.dialogVisible = true
      this.subForm = JSON.parse(JSON.stringify(row))
    },
    clickCity (e) {
      this.showMap = true
    },
    getLatLng (latLng) {
      console.log('latLng', latLng)
      this.subForm.lng = latLng.lng
      this.subForm.lat = latLng.lat
    },
    addCityList () {
      this.subForm = {
        title: '',
        lng: '',
        lat: ''
      }
      this.dialogVisible = true
    },
    confirmDel (row) {
      this.$confirm(
        this.$t('tips.confirmDelete'),
        this.$t('tips.reminder'),
        {
          confirmButtonText: this.$t('action.comfirm'),
          cancelButtonText: this.$t('action.cancel'),
          type: 'warning'
        }
      ).then(() => {
        this.$api.system
          .cityUpdate({ id: row.id, status: '-1' })
          .then((res) => {
            this.$message.success(this.$t('tips.successOper'))
            this.getList('init')
          })
      })
    },
    async getList (e) {
      if (e === 'init') {
        this.searchForm = {
          page: 1,
          limit: 10
        }
      }
      this.loading = true
      let { code, data } = await this.$api.system.cityList(
        this.searchForm
      )
      this.loading = false
      if (code !== 200) return
      this.cityList = data.data
      this.total = data.total
    },
    submitForm () {
      console.log('this.subForm', this.subForm)
      this.$refs['subForm'].validate((valid) => {
        if (valid) {
          let { subForm } = this
          let type = subForm.id ? 'cityUpdate' : 'cityAdd'
          this.$api.system[type]({ status: 1, ...subForm }).then(
            (res) => {
              if (res.code === 200) {
                this.$message.success(
                  this.$t('tips.successSub')
                )
                this.dialogVisible = false
                this.getList('init')
              }
            }
          )
        }
      })
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.f_c {
  display: flex;
  flex-direction: column;
}
.f_r_m_c {
  display: flex;
  align-items: center;
}
.lb-system-transaction {
  width: 100%;
  .page-main {
    padding: 20px;
    .el-form {
      width: 100%;
      .el-form-item {
        margin-bottom: 24px;
        .el-select,
        .el-input-number,
        .el-input {
          width: 300px;
        }
      }
      .last-form-item {
        margin-top: 30px;
      }
      .item-tips {
        margin-left: 120px;
        margin-bottom: 24px;
        color: #999999;
      }
    }
  }
}
</style>
